//-----------------------------------------------------------------------
// <copyright file="SendEmailDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class SendEmailDAOBase
    {
        #region Common methods
        public virtual SendEmail CreateSendEmailFromReader(IDataReader reader)
        {
            SendEmail item = new SendEmail();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("SendEmailId"))) item.SendEmailId = (long)reader["SendEmailId"];
                if (!reader.IsDBNull(reader.GetOrdinal("MemberId"))) item.MemberId = (long)reader["MemberId"];
                if (!reader.IsDBNull(reader.GetOrdinal("RecipientEmail"))) item.RecipientEmail = (string)reader["RecipientEmail"];
                if (!reader.IsDBNull(reader.GetOrdinal("Status"))) item.Status = (bool)reader["Status"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateSendEmailFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateSendEmail methods
            
        public virtual void CreateSendEmail(SendEmail sendEmail)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spSendEmailInsert");
                
                database.AddInParameter(dbCommand, "@MemberId", DbType.Int64, sendEmail.MemberId);
                database.AddInParameter(dbCommand, "@RecipientEmail", DbType.String, sendEmail.RecipientEmail);
                database.AddInParameter(dbCommand, "@Status", DbType.Boolean, sendEmail.Status);
                database.AddOutParameter(dbCommand, "@SendEmailId", DbType.Int64, 8);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                sendEmail.SendEmailId = (long)database.GetParameterValue(dbCommand, "@SendEmailId");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateSendEmailException, ex);
            }
        }

        #endregion

        #region UpdateSendEmail methods
        
        public virtual void UpdateSendEmail(SendEmail sendEmail)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spSendEmailUpdate");            
                
                database.AddInParameter(dbCommand, "@SendEmailId", DbType.Int64, sendEmail.SendEmailId);
                database.AddInParameter(dbCommand, "@MemberId", DbType.Int64, sendEmail.MemberId);
                database.AddInParameter(dbCommand, "@RecipientEmail", DbType.String, sendEmail.RecipientEmail);
                database.AddInParameter(dbCommand, "@Status", DbType.Boolean, sendEmail.Status);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateSendEmailException, ex);
            }
        }
        
        #endregion

        #region DeleteSendEmail methods
        public virtual void DeleteSendEmail(long sendEmailId)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spSendEmailDelete");
                
                database.AddInParameter(dbCommand, "@SendEmailId", DbType.Int64, sendEmailId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteSendEmailException, ex);
            }
        }

        #endregion

        #region GetSendEmail methods
        
        public virtual SendEmail GetSendEmail(long sendEmailId)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spSendEmailGet");
                
                database.AddInParameter(dbCommand, "@SendEmailId", DbType.Int64, sendEmailId);
                
                SendEmail sendEmail =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        sendEmail = CreateSendEmailFromReader(reader);
                        reader.Close();
                    }
                }
                return sendEmail;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetSendEmailException, ex);
            }
        }
        
        #endregion

        #region GetSendEmailList methods
        public virtual SendEmailCollection GetSendEmailList(SendEmailColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spSendEmailGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                SendEmailCollection sendEmailCollection = new SendEmailCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        SendEmail sendEmail = CreateSendEmailFromReader(reader);
                        sendEmailCollection.Add(sendEmail);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return sendEmailCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetSendEmailListException, ex);
            }
        }
        
        public virtual SendEmailCollection GetSendEmailList(SendEmailColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetSendEmailList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetSendEmailList methods
        public virtual SendEmailCollection GetSendEmailListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spSendEmailGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                SendEmailCollection sendEmailCollection = new SendEmailCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        SendEmail sendEmail = CreateSendEmailFromReader(reader);
                        sendEmailCollection.Add(sendEmail);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return sendEmailCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetSendEmailListException, ex);
            }
        }
        
        public virtual SendEmailCollection GetSendEmailListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetSendEmailListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}